package cn.labbook.bluetooth.service.impl;

import cn.labbook.bluetooth.common.MyPage;
import cn.labbook.bluetooth.constant.TenantPayConstant;
import cn.labbook.bluetooth.entity.TenantPay;
import cn.labbook.bluetooth.mapper.TenantPayMapper;
import cn.labbook.bluetooth.service.ITenantPayService;
import cn.labbook.bluetooth.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lonzy
 * @since 2019-03-26
 */
@Service
public class TenantPayServiceImpl extends ServiceImpl<TenantPayMapper, TenantPay> implements ITenantPayService {

    @Override
    public List<TenantPay> getGuestRecharge(String Date, String userId, MyPage<TenantPay> page) {
        QueryWrapper<TenantPay> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(Date)) {
            wrapper.likeRight("tp.pay_time", Date);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            wrapper.likeRight("tp.pay_time", sdf.format(new Date()));
        }
        wrapper.eq("tp.pay_status", TenantPayConstant.YES)
                .eq("at.manager_id", userId)
                .orderByDesc("tp.pay_time");
        return this.baseMapper.geGuestRecharge(wrapper, page);
    }

    @Override
    public List<TenantPay> getSumPay(String Date, String userId) {
        QueryWrapper<TenantPay> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(Date)) {
            wrapper.likeRight("tp.pay_time", Date);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            wrapper.likeRight("tp.pay_time", sdf.format(new Date()));
        }
        wrapper.eq("tp.is_arrive", TenantPayConstant.YES)
                .eq("at.manager_id", userId);
        return this.baseMapper.getSumPay(wrapper);
    }

    @Override
    public Integer getIntercept(String ammeterId) {
        return this.baseMapper.getIntercept(ammeterId);
    }
}

